﻿Imports System.Data.SqlClient

Public Class frmClientEdit

    Public SNo As Integer

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Sub FillProducersList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim ProducersList As ArrayList = GetProducersList()

            Me.CombProducers.Items.Clear()

            For Each InsurComp As String In ProducersList
                Me.CombProducers.Items.Add(InsurComp)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillBranches()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Branches As ArrayList = GetBranches()

            For Each Branch As String In Branches
                Me.CombDirectBusiness.Items.Add("Direct Business - " & Branch)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmClientRegistryEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillBranches()
        FillProducersList()

        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From Clients Where SNo=" & SNo, cnn)
            Dim Reader As SqlDataReader
            Dim Producer As String

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.txtClientName.Text = Reader.Item("FullName")
                Producer = Reader.Item("Producer")
                Me.txtTele.Text = Reader.Item("Tele")
                Me.txtAddress.Text = Reader.Item("Address")
            End While
            cnn.Close()

            If Producer.Contains("Direct Business - ") Then
                Me.CombDirectBusiness.SelectedItem = Producer
                Me.RDirectBusiness.Checked = True
                Me.CombProducers.Enabled = False
            Else
                Me.RProducer.Checked = True
                Me.CombProducers.Enabled = True
                Me.CombProducers.SelectedItem = Producer
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If Me.txtClientName.Text.Trim.Length = 0 Then
                MsgBox("Please fill in all mandatory fields")
                Exit Sub
            ElseIf Me.RDirectBusiness.Checked = True And Me.CombDirectBusiness.SelectedIndex = -1 Then
                MsgBox("Please select a valid producer from the list")
            ElseIf Me.RProducer.Checked = True And Me.CombProducers.SelectedIndex = -1 Then
                MsgBox("Please select a valid producer from the list")
            Else
                Me.Cursor = Cursors.WaitCursor

                Dim Producer As String
                If Me.RDirectBusiness.Checked = True Then
                    Producer = Me.CombDirectBusiness.SelectedItem

                ElseIf Me.RProducer.Checked = True Then
                    Producer = Me.CombProducers.SelectedItem
                End If

                Dim cmd As New SqlCommand("Update Clients " & _
                                          "Set FullName=@FullName,Producer=@Producer,Tele=@Tele,Address=@Address Where SNo=" & SNo, cnn)

                cnn.Open()

                'Add values
                cmd.Parameters.AddWithValue("@FullName", Me.txtClientName.Text.Trim)
                cmd.Parameters.AddWithValue("@Producer", Producer)
                cmd.Parameters.AddWithValue("@Tele", Me.txtTele.Text.Trim)
                cmd.Parameters.AddWithValue("@Address", Me.txtAddress.Text.Trim)

                cmd.ExecuteNonQuery()
                cnn.Close()

                Me.Cursor = Cursors.Default

                Me.Close()
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub CheckRadio()
        If Me.RDirectBusiness.Checked = True Then
            Me.CombDirectBusiness.Enabled = True
            Me.CombProducers.SelectedIndex = -1
            Me.CombProducers.Enabled = False

        ElseIf Me.RProducer.Checked = True Then
            Me.CombDirectBusiness.Enabled = False
            Me.CombDirectBusiness.SelectedIndex = -1
            Me.CombProducers.Enabled = True
        End If
    End Sub

    Private Sub RDirectBusiness_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RDirectBusiness.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub RProducer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RProducer.CheckedChanged
        CheckRadio()
    End Sub
End Class